package com.corticon.dialogs.util.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.util.Assert;

import com.corticon.dialogs.model.PermissionType;

/**
 * 
 * 
 * @author Yanglifan
 */
public class SecurityUtils {

    /**
     * Get the current user permission(authority). By default,
     * {@link SecurityContextHolder} get the {@link Authentication} object from
     * ThreadLocal.
     *
     * If can not get any permission info, return empty list.
     *
     * @return
     */
    public static List<String> getCurrentUserPermissions() {
        List<String> authorityList = new ArrayList<String>();

        // Get the authentication object.
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null) {
            GrantedAuthority[] authorities = authentication.getAuthorities();
            if (authorities != null && authorities.length > 0) {
                for (GrantedAuthority authority : authorities) {
                    // Put the authority infos into List;
                    authorityList.add(authority.getAuthority());
                }
            }
        }
        return authorityList;
    }

    public static CustomUserDetails getCurrentUser() {
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof CustomUserDetails) {
            CustomUserDetails user = (CustomUserDetails) obj;
            user.setPermissions(getCurrentUserPermissions());
            return user;
        }
        return null;
    }

    public static PermissionType checkCurrentAdminPermission(CustomUserDetails currentUser) {
        Assert.notNull(currentUser);

        for (GrantedAuthority ga : currentUser.getAuthorities()) {
            if (PermissionType.GROUP_ADMIN.value().equalsIgnoreCase(
                ga.getAuthority())) {
                return PermissionType.GROUP_ADMIN;
            }
            if (PermissionType.SYS_ADMIN.value().equalsIgnoreCase(
                ga.getAuthority())) {
                return PermissionType.SYS_ADMIN;
            }
            if (PermissionType.AUTHOR.value().equalsIgnoreCase(
                ga.getAuthority())) {
                return PermissionType.AUTHOR;
            }
            if (PermissionType.PORTAL_USER.value().equalsIgnoreCase(
                ga.getAuthority())) {
                return PermissionType.PORTAL_USER;
            }
            if (PermissionType.REVIEWER.value().equalsIgnoreCase(
                ga.getAuthority())) {
                return PermissionType.REVIEWER;
            }
        }
        return null;
    }

    /**
     * This method checked the permission if the user have the permission which
     * you set.
     *
     * @param strs
     *            This parameter is GrantedAuthoritys which current user have.
     * @param s
     *            This parameter is the permission which you want to find.
     * @return If exist return true, else return false.
     */
    public static boolean isHave(GrantedAuthority[] strs, String s) {

        for (int i = 0; i < strs.length; i++) {
            if (strs[i].getAuthority().equalsIgnoreCase(s)) {
                return true;
            }
        }
        return false;
    }
}
